const db = require('./db');

function Category(category) {
    this.id = category.id;
    this.categoryName = category.categoryName;
    this.isDelete = category.isDelete;
};

Category.queryTopCategory = (callback) => {
    var selectSql = "select * from category where isDelete = 1";
    db.query(selectSql, (err, result) => {
        if (err) {
            return callback(err);
        }
        let data = result;
        callback(err, data);
    });

}
Category.querySecondCategory = (id, callback) => {
    let selectSql = "select * from brand where categoryId =? and isDelete=1";
    db.query(selectSql, [id], (err, result) => {
        if (err) {
            return callback(err);
        }
        var data = result;
        callback(err, data);
    })
}
Category.queryTopCategoryPaging = (page, callback) => {
    let selectSql = "select * from category order by id desc";
    selectSql += " limit ?,?";
    db.query(selectSql, [(page.page - 1) * page.size, page.size], (err, result) => {
        if (err) return callback(err);
        let data = result;
        callback(err, data);
    })
}
Category.countTopCategory = (callback) => {
    let selectSql = "select count(*) as count from category ";
    db.query(selectSql, (err, result) => {
        if (err) return callback(err);
        let data = result[0];
        callback(err, data);
    })
}
Category.updateTopCategory = (category, callback) => {
    let selectSql = "update category set";
    let params = new Array();
    if (category.categoryName) { // 修改分类名称
        selectSql += " categoryName=? ";
        params[params.length] = category.categoryName;
    }
    if (category.isDelete && params.length == 0) { // 只修改分类状态
        selectSql += " isDelete=? ";
        params[params.length] = category.isDelete;
    } else if (category.isDelete && params.length != 0) {
        selectSql += " ,isDelete=? ";
        params[params.length] = category.isDelete;
    }
    selectSql += " where id =?";
    params[params.length] = category.id;

    db.query(selectSql, params, (err, result) => {
        if (err) return callback(err);
        callback(err, result);
    })
}
Category.addTopCategory = (category, callback) => {
    let selectSql = "insert into category (id, categoryName, isDelete) values (null,?,1)";
    db.query(selectSql, [category.categoryName], (err, result) => {
        if (err) return callback(err);
        callback(err, result);
    })
}
Category.querySecondCategoryPaging = (page, callback) => {
    let selectSql = "SELECT b.*,c.categoryName FROM brand AS b LEFT JOIN category AS c ON b.categoryId=c.id ORDER BY b.id DESC";
    selectSql += " LIMIT ?,?";
    db.query(selectSql, [(page.page - 1) * page.size, page.size], (err, result) => {
        if (err) return callback(err);
        callback(err, result);
    })
}
Category.countSecondCategory = (callback) => {
    let selectSql = "SELECT count(*) AS count FROM brand";
    db.query(selectSql, (err, result) => {
        if (err) return callback(err);
        callback(err, result[0]);
    })
}

module.exports = Category;